﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.WorNum;

namespace SmartProduction.Write.API.Application.CommandHandlers.WorkNum
{
    /// <summary>
    /// 工序删除命令处理类
    /// </summary>
    public class WorkNumDeleteCommandHandler:IRequestHandler<WorkNumDeleteCommand,APIResult<object>>
    {
        /// <summary>
        /// 工序仓储
        /// </summary>
        private readonly IBaseRepository<Domain.yjh.Process> prorpe;
        /// <summary>
        /// mapper映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="prorpe">工序仓储</param>
        /// <param name="mapper">mapper映射</param>
        public WorkNumDeleteCommandHandler(IBaseRepository<Domain.yjh.Process> prorpe, IMapper mapper)
        {
            this.prorpe = prorpe;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">实体</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回的任务</returns>
        public async Task<APIResult<object>> Handle(WorkNumDeleteCommand request, CancellationToken cancellationToken)
        {
          APIResult<object> reuslt=new APIResult<object>();

            var entity = prorpe.GetAll().Result.FirstOrDefault(x => x.ProcessId == request.processId);
            entity.IsDeleted= true;
            var r = prorpe.UpdateAsync(entity).Result;

            if (r > 0)
            {
                LogModel logModel = new LogModel();
                logModel.CreateDate = DateTime.Now;
                logModel.CreatePerson = "YJH";
                logModel.LoginThing = "正在删除工序";
                reuslt.Code = APIEnums.删除成功;
                reuslt.Msg = APIEnums.删除成功.ToString();
                reuslt.data = r;
            }
            else
            {
                reuslt.Code = APIEnums.删除失败;
                reuslt.Msg = APIEnums.删除失败.ToString();
                reuslt.data = r;

            }

            return await Task.FromResult(reuslt);

        }
    }
}
